from rest_framework import serializers
from .models import *



# 用户
class UserSer(serializers.ModelSerializer):
    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    r_name = serializers.SerializerMethodField()

    def get_r_name(self,obj):
        return obj.role.r_name

    class Meta:
        model = UserModel
        exclude=('password',)

# 学校
class SchoolSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    username = serializers.SerializerMethodField()

    def get_username(self,obj):
        return obj.user.username

    class Meta:
        model = SchoolModel
        fields = '__all__'

# 菜单管理
class MenuSer(serializers.ModelSerializer):
    class Meta:
        model = MenuModel
        fields = '__all__'


# 班级
class ClassSer(serializers.ModelSerializer):
    teacher = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    teacher_name = serializers.SerializerMethodField()
    school = serializers.PrimaryKeyRelatedField(queryset=SchoolModel.objects.all())
    school_name = serializers.SerializerMethodField()

    def get_teacher_name(self,obj):
        return obj.teacher.username

    def get_school_name(self,obj):
        return obj.school.s_name
    class Meta:
        model = ClassModel
        fields = '__all__'


# 老师
class TeachersSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    username = serializers.SerializerMethodField()

    school = serializers.PrimaryKeyRelatedField(queryset=SchoolModel.objects.all())
    school_name = serializers.SerializerMethodField()

    def get_username(self,obj):
        return obj.user.username

    def get_school_name(self,obj):
        return obj.school.s_name

    class Meta:
        model = TeachersModel
        fields = '__all__'


# 学生
class StudentsSer(serializers.ModelSerializer):
    cla = serializers.PrimaryKeyRelatedField(queryset=ClassModel.objects.all())
    c_name = serializers.SerializerMethodField()

    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    username = serializers.SerializerMethodField()

    def get_c_name(self,obj):
        return obj.cla.c_name

    def get_username(self,obj):
        return obj.user.username

    class Meta:
        model = StudentsModel
        fields = '__all__'


# 问候语序列化器
class GreetingsSer(serializers.ModelSerializer):

    class Meta:
        model = Greetings
        fields = "__all__"



# 分类
class SortSer(serializers.ModelSerializer):

    class Meta:
        model = Sort
        fields = "__all__"



# 标签
class LabelSer(serializers.ModelSerializer):

    class Meta:
        model = Label
        fields = "__all__"

# 学习内容
class Learning_contentSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    username = serializers.SerializerMethodField()

    def get_username(self,obj):
        return obj.user.username

    class Meta:
        model = Learning_content
        fields = "__all__"



# 学习资源
class ResourceSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    username = serializers.SerializerMethodField()

    def get_username(self,obj):
        return obj.user.username

    class Meta:
        model = Resource
        fields = "__all__"


# 章节
class ChaptersSer(serializers.ModelSerializer):
    resource = serializers.PrimaryKeyRelatedField(queryset=Resource.objects.all())
    r_name = serializers.SerializerMethodField()

    def get_r_name(self,obj):
        return obj.resource.r_name

    class Meta:
        model = ChaptersModel
        fields = "__all__"


# 章节
class SectionsSer(serializers.ModelSerializer):
    chapter = serializers.PrimaryKeyRelatedField(queryset=ChaptersModel.objects.all())
    c_name = serializers.SerializerMethodField()

    def get_c_name(self,obj):
        return obj.chapter.title

    class Meta:
        model = SectionsModel
        fields = "__all__"




# 章节
class Learning_TaskSer(serializers.ModelSerializer):
    content = serializers.PrimaryKeyRelatedField(queryset=Learning_content.objects.all())
    c_name = serializers.SerializerMethodField()

    def get_c_name(self,obj):
        return obj.content.c_name

    class Meta:
        model = Learning_TaskModel
        fields = "__all__"


# 评论
class CommentSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    username = serializers.SerializerMethodField()
    content = serializers.PrimaryKeyRelatedField(queryset=Learning_content.objects.all())
    c_name = serializers.SerializerMethodField()

    def get_c_name(self, obj):
        return obj.content.c_name

    def get_username(self, obj):
        return obj.user.username


    class Meta:
        model = Comment
        fields = "__all__"




